﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Data;
using Newtonsoft.Json.Linq;

namespace Test_API
{
    class AssessmentTest : GenericModuleTest
    {
        protected override string moduleName
        {
            get
            {
                return "Assignment Test";
            }
        }

        private string addAssignmentMethod;
        private string provideSolutionMethod;
        private string deleteSolutionMethod;

        public AssessmentTest(string accessToken, string cid)
            : base(accessToken)
        {
            this.viewAllMethod = "viewAllAssignments";
            this.deleteMethod = "deleteAssignment";
            this.provideSolutionMethod = "provideAssignmentSolution";
            this.deleteSolutionMethod = "deleteAssignmentSolution";
            this.addAssignmentMethod = "addAssignment";
            this.cid = cid;
            this.uploadMethod = "uploadInAssignments";
        }

        protected override string genericQueryStringGenerate(int validItemID)
        {
            string queryString = new Dictionary<string, string>() { { "cid", this.cid }, { "accesstoken", this.accesstoken }, { "itemid", validItemID.ToString() }, { "assignmentid", validItemID.ToString() }, { "gws_name_alias", Library.getWord() } }.buildQueryString();

            return queryString;
        }
        protected override Dictionary<string, object> genericPostDataSetGenerate()
        {
            Dictionary<string, object> dataSet = new Dictionary<string, object>();
            {
                dataSet.Add("comment", Library.getDescription());
                dataSet.Add("title", Library.getWord());
                dataSet.Add("description", Library.getDescription());
                dataSet.Add("totalMarks", ExtenstionMethods.randomNumber(50, 100));
                dataSet.Add("dueDate", DateTime.Now.AddMonths(1).convertToUnixtime());
            }
            return dataSet;
        }

        private List<LogEntry> provideAssignmentSolution()
        {
            List<LogEntry> entries = new List<LogEntry>();
            LogEntry t1 = new LogEntry(this.moduleName, true);
            HTTP.execute(HTTP.RequestType.POST, this.rootUrl, this.provideSolutionMethod, this.genericQueryStringGenerate(this.validItemIDs.randomElement()), this.genericPostDataSetGenerate(), ref t1);
            entries.Add(t1);

            return entries;
        }

        private List<LogEntry> deleteSolution()
        {
            List<LogEntry> viewEntries = this.viewAll();
            LogEntry t1 = new LogEntry(this.moduleName, true);

            JObject receievedData = JObject.Parse(viewEntries[0].receivedData);
            JArray dataSet = (JArray)receievedData["dataSet"];
            JObject randomAssignment = (JObject)dataSet.ToList().randomElement();

            List<LogEntry> entries = new List<LogEntry>();
            if (randomAssignment != null)
            {
                if (randomAssignment["solution"].Count() == 0)
                {
                    t1.methodName = this.deleteSolutionMethod;
                    t1.receivedData = "/* No solutions to delete, please provide a solution in assignment " + (int)randomAssignment["itemId"] + " */";
                    t1.inputs = null;
                    t1.verdict = true;
                    entries.Add(t1);
                    return entries;
                }
                int solutionId = (int)randomAssignment["solution"]["itemId"];

                LogEntry t2 = new LogEntry(this.moduleName, true);
                HTTP.execute(HTTP.RequestType.GET, this.rootUrl, this.deleteSolutionMethod, this.genericQueryStringGenerate(solutionId), null, ref t2);
                entries.Add(t2);
            }
            return entries;
        }

        protected List<LogEntry> addOrUpdate(bool doAddOperation)
        {
            List<LogEntry> entries = new List<LogEntry>();
            string methodUrl, queryString;

            LogEntry t1 = new LogEntry(this.moduleName, true);
            if (doAddOperation == true)
            {
                methodUrl = this.addAssignmentMethod;
                queryString = new Dictionary<string, string>() { { "accessToken", this.accesstoken }, { "cid", this.cid } }.buildQueryString();
            }
            else
            {
                throw new Exception("Not yet available");
            }
            HTTP.execute(HTTP.RequestType.POST, this.rootUrl, methodUrl, queryString, this.genericPostDataSetGenerate(), ref t1);
            t1.verdict &= API_VERDICT.checkAddOrUpdateOperation(t1.receivedData, doAddOperation);
            entries.Add(t1);
            return entries;
        }

        public List<LogEntry> start(string localFile)
        {
            Console.WriteLine("Assignment API call begin\n============");
            List<LogEntry> entries = new List<LogEntry>();

            entries.AddRange(this.viewAll());
            entries.AddRange(this.provideAssignmentSolution());
            entries.AddRange(this.deleteSolution());
            entries.AddRange(this.delete());
            entries.AddRange(this.uploadFile(localFile, 6, this.uploadMethod));
            entries.AddRange(this.addOrUpdate(true));
            return entries;
        }
    }
}